package dirtyWordFilter;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class KeyWordPatterFilter {
	
	public final List<String> wordList;
	public final List<String> patternList;
	
	public KeyWordPatterFilter(List<String> wordList){
		this.wordList = wordList; 
		patternList = new ArrayList<String>();
	}
	
	public boolean filter(String inputString){
		for(String str : patternList){
			Pattern pattern = Pattern.compile(str);
			Matcher matcher = pattern.matcher(inputString);
			if(matcher.find()){
				return true;
			}
		}
		return false;
	}
	
	public void init(){
		String pattern = null;
		for(String str : wordList){
			if(!str.contains("*")){
				pattern = "\\b(" + str + ")\\b";
			}else if(str.startsWith("*")){
				pattern = str.replace("*", ".*");
			} else {
				pattern = str.replace("*", ".*");
				pattern = "\\b" + pattern;
			}
		}
		patternList.add(pattern);
	}
	
	
	

}
